package com.yeyks.task;

import com.github.pagehelper.PageHelper;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.hotel.dal.dao.RoomInfoMapper;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yeyks.hotel.service.RoomCalendarInfoService;
import com.yeyks.hotel.service.RoomCalendarVipInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @Author fuchangshuai
 * @date 2019/3/23 3:48 PM
 */
//@Component("RoomTask")
//@Slf4j
public class RoomTask {

//    @Autowired
//    private RoomInfoMapper roomInfoMapper;
//    @Autowired
//    private RoomCalendarInfoService roomCalendarInfoService;
//    @Autowired
//    private RoomCalendarVipInfoService roomCalendarVipInfoService;

//    /**
//     * 每天凌晨执行一次，并将available修改为count
//     */
//    @Scheduled(cron = "${schedule.room.available}")//每天凌晨2分和4分执行一次
//    @Transactional
//    public void checkAvailable() {
//        log.info("更新available开始begin......");
//        //查询available为0的房间数
//        List<RoomInfo> roomInfos = roomInfoMapper.selectRoomByAvailable();
//        for (RoomInfo roomInfo : roomInfos) {
//            Date nowDate = DateUtils.toDate(LocalDate.now());
//            Date todayTime = roomInfo.getTodayTime();
//            if (todayTime == null) {
//                todayTime = nowDate;
//                roomInfo.setTodayTime(todayTime);
//                roomInfo.setAvailable(roomInfo.getCount());
//                roomInfoMapper.updateIfNotNullByPrimaryKey(roomInfo);
//            }
//            if (nowDate.compareTo(todayTime) > 0) {
//                roomInfo.setAvailable(roomInfo.getCount());
//                roomInfo.setTodayTime(nowDate);
//                roomInfoMapper.updateIfNotNullByPrimaryKey(roomInfo);
//            }
//
//        }
//        log.info("更新available结束");
//    }

//    @Scheduled(cron = "${schedule.room.createRoomCalendarInfo}")
//    @Scheduled(fixedDelay = 1 * 60 * 60 * 1000)
//    public void createRoomCalendarInfo() {
//        log.info("普通房间日历开始更新begin......");
//        List<RoomInfo> roomInfos = null;
//        //页数
//        int page = 1;
//        //循环添加
//        do {
//            PageHelper.startPage(page++, 100);
//            roomInfos = roomInfoMapper.selectNormalRoom();
//            if (roomInfos != null && roomInfos.size() > 0) {
//                roomCalendarInfoService.createRoomCalendarInfoDo(roomInfos);
//            }
//        } while (roomInfos != null && roomInfos.size() > 0);
//        log.info("普通房间日历结束更新end......");
//    }
//
//    @Scheduled(cron = "${schedule.room.createRoomCalendarInfo}")
//    @Scheduled(fixedDelay = 1 * 60 * 60 * 1000)
//    public void createRoomCalendarVipInfo() {
//        log.info("VIP房间日历开始更新begin......");
//        List<RoomInfo> roomInfos = null;
//        //页数
//        int page = 1;
//        //循环添加
//        do {
//            PageHelper.startPage(page++, 100);
//            roomInfos = roomInfoMapper.selectVipRoom();
//            if (roomInfos != null && roomInfos.size() > 0) {
//                roomCalendarVipInfoService.createRoomCalendarVipInfoDo(roomInfos);
//            }
//        } while (roomInfos != null && roomInfos.size() > 0);
//        log.info("VIP房间日历结束更新end......");
//    }

}
